package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.ExamService;
import cc.eddic.pratice.programmingparadigms.exam.MasterDataService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Exam;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.*;

public class ExamServiceCxmImpl implements ExamService {

    @Override
    public List<Answer> importAnswers(String answerExcelFile) {
        MasterDataService masterDataService=new MasterDataServiceCxmImpl();
        QuestionBankService questionBankService=new QuestionBankServiceCxmImpl();
        List<Map<Integer,String>> resultList = new ArrayList<>();

        Map<Integer, String> newHeadMap = new HashMap<>();
        EasyExcel.read(answerExcelFile, Student.class, new AnalysisEventListener<Student>() {

            @Override
            public void invoke(Student student, AnalysisContext analysisContext) {

            }

            @Override
            public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
                System.out.println(headMap);
                newHeadMap.putAll(headMap);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet(1).doRead();

        Set<Integer> headKeys = newHeadMap.keySet();
        Integer headFirstKey = headKeys.iterator().next();
        newHeadMap.remove(headFirstKey);

        List<String> nameList = new ArrayList<>();
        for (Integer key : newHeadMap.keySet()) {
            String name = newHeadMap.get(key);
            nameList.add(name);
        }

        EasyExcel.read(answerExcelFile,new AnalysisEventListener<Map<Integer,String>>() {
            @Override
            public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
                resultList.add(map);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet(1).doRead();

        Iterator<Map<Integer,String>> resultIterator  = resultList.iterator();
        List<Map<Integer,String>> newResultList=new ArrayList<>();
        while(resultIterator.hasNext()){
            Map<Integer,String> map = resultIterator.next();
            Set<Integer> keys=map.keySet();
            Integer firstKey=keys.iterator().next();
            map.remove(firstKey);
            newResultList.add(map);
        }
        Exam exam = masterDataService.find(masterDataService.getExams(), exam1 -> "7-12 开营考试".equals(exam1.getName())).
                orElseThrow(() -> new IllegalArgumentException(""));
        List<Question> questionList = questionBankService.importQuestions(" ");
        List<Answer> answerList=new ArrayList<>();
        Student student;
        Question question;

        int i=0,j=0;
        for(Map<Integer,String> map:newResultList){
            int finalI=i,finalJ=j;
            for(Integer key: map.keySet()){
                  student=masterDataService.getStudents().stream().filter(s -> s.getName().equals(nameList.get(finalI))).findFirst()
                        .orElseThrow(() -> new IllegalArgumentException("找不到"));
                  question = masterDataService.find(questionList, x -> questionList.get(finalJ).getNo() == x.getNo())
                        .orElseThrow(() -> new IllegalArgumentException("找不到"));
                  Answer answer=new Answer(exam,question,student,map.get(key).toCharArray()[0]);
                  answerList.add(answer);
            }
            i++;
            j++;
        }
        return answerList;
    }




}
